package com.rong.DAO;

import com.opensymphony.xwork2.ActionContext;
import com.rong.hibernate.BaseHibernateDAO;
import com.rong.hibernate.Const;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;

import com.rong.ORM.ZynPrice1;
import com.rong.ORM.ZynPrice2;
import com.rong.ORM.ZynQueryPrice;
import com.rong.ORM.ZynUser;

/**
 * A data access object (DAO) providing persistence and search support for
 * ZynPrice2 entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.rong.DAO.ZynPrice2
 * @author MyEclipse Persistence Tools
 */

public class ZynPrice2DAO extends BaseHibernateDAO {
	private static final Log log = LogFactory.getLog(ZynPrice2DAO.class);
	// property constants
	public static final String COM_ID = "comId";
	public static final String CAR_ID = "carId";
	public static final String PRICE = "price";
	public static final String PIECE = "piece";
	public static final String REMARK = "remark";
	public static final String EXTATTR1 = "extattr1";
	public static final String EXTATTR2 = "extattr2";
	public static final String EXTATTR3 = "extattr3";
	public static final String EXTATTR4 = "extattr4";
	public static final String EXTATTR5 = "extattr5";

	public void save(ZynPrice2 transientInstance) {
		log.debug("saving ZynPrice2 instance");
		try {
			 Transaction ts = null;
			 ts = getSession().beginTransaction();
			getSession().save(transientInstance);
			ts.commit();
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(ZynPrice2 persistentInstance) {
		log.debug("deleting ZynPrice2 instance");
		try {
			Transaction ts = null;
			 ts = getSession().beginTransaction();
			getSession().delete(persistentInstance);
			ts.commit();
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public ZynPrice2 findById(java.lang.Integer id) {
		log.debug("getting ZynPrice2 instance with id: " + id);
		try {
			ZynPrice2 instance = (ZynPrice2) getSession().load(ZynPrice2.class, id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(ZynPrice2 instance) {
		log.debug("finding ZynPrice2 instance by example");
		try {
			List results = getSession()
					.createCriteria("com.rong.DAO.ZynPrice2").add(
							Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding ZynPrice2 instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from ZynPrice2 as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByComId(Object comId) {
		return findByProperty(COM_ID, comId);
	}

	public List findByCarId(Object carId) {
		return findByProperty(CAR_ID, carId);
	}

	public List findByPrice(Object price) {
		return findByProperty(PRICE, price);
	}

	public List findByPiece(Object piece) {
		return findByProperty(PIECE, piece);
	}

	public List findByRemark(Object remark) {
		return findByProperty(REMARK, remark);
	}

	public List findByExtattr1(Object extattr1) {
		return findByProperty(EXTATTR1, extattr1);
	}

	public List findByExtattr2(Object extattr2) {
		return findByProperty(EXTATTR2, extattr2);
	}

	public List findByExtattr3(Object extattr3) {
		return findByProperty(EXTATTR3, extattr3);
	}

	public List findByExtattr4(Object extattr4) {
		return findByProperty(EXTATTR4, extattr4);
	}

	public List findByExtattr5(Object extattr5) {
		return findByProperty(EXTATTR5, extattr5);
	}

	public List findAll() {
		log.debug("finding all ZynPrice2 instances");
		try {
			String queryString = "from ZynPrice2";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public ZynPrice2 merge(ZynPrice2 detachedInstance) {
		log.debug("merging ZynPrice2 instance");
		try {
			Transaction ts = null;
			 ts = getSession().beginTransaction();
			ZynPrice2 result = (ZynPrice2) getSession().merge(detachedInstance);
			ts.commit();
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(ZynPrice2 instance) {
		log.debug("attaching dirty ZynPrice2 instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(ZynPrice2 instance) {
		log.debug("attaching clean ZynPrice2 instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
	

	/**
	 * 查找并返回符合条件的价格记录
	 */

	public List<ZynPrice2> findPriceQueryByPage(int page, int rowsPerPage,ZynQueryPrice queryCrt) {
		
		ActionContext actionContext = ActionContext.getContext();         
	     Map session = actionContext.getSession(); 
	     ZynUser loginUser=(ZynUser)session.get("loginUser");   //登录用户	
	
		Transaction ts2=null;
		ts2=getSession().beginTransaction();
		getSession().clear();
				
		Criteria crt = getSession().createCriteria(ZynPrice2.class);
		  Criteria crtCom = crt.createCriteria("comObj");
		crt.addOrder(org.hibernate.criterion.Order.desc("id"));
		
		if(queryCrt.getStartDate()!=null&&queryCrt.getEndDate()!=null){
			crt.add(Restrictions.and(Restrictions.ge("invalidationDate", queryCrt.getStartDate()), Restrictions.le("invalidationDate", queryCrt.getEndDate())));
		}else if(queryCrt.getStartDate()!=null&&queryCrt.getEndDate()==null){
			crt.add(Restrictions.ge("invalidationDate", queryCrt.getStartDate()));	
		}else if(queryCrt.getStartDate()==null&&queryCrt.getEndDate()!=null){
			crt.add(Restrictions.le("invalidationDate", queryCrt.getEndDate()));	
		}
		
		if(queryCrt.getFromComName()!=null&&!queryCrt.getFromComName().equalsIgnoreCase("")){ //公司
			
			crtCom.add(Restrictions.like("comName", queryCrt.getFromComName(),MatchMode.ANYWHERE));
		}
	
		if(queryCrt.getPrice()!=null&&queryCrt.getPrice()>0){ //价格
			crt.add(Restrictions.ge("price", queryCrt.getPrice()));	
		}
				
		//--权限判断,分公司只能看本公司单据，本部可以查看全部		
		if(loginUser.getZyncom().getComProperty().equalsIgnoreCase(Const.comTypetop)){			
			
		}else{			
					//所属分公司
			crtCom.add(Restrictions.eq("extAttr3", String.valueOf(loginUser.getComId())));
			  //匹配单据的所属公司
		}
		
		
		crt.setMaxResults(rowsPerPage); // 每页最多显示几条
		crt.setFirstResult((page - 1) * rowsPerPage); // 每页从第几条记录开始
		ts2.commit();
		List<ZynPrice2> list = crt.list();
		for (int i = 0; i < list.size(); i++) {
			System.out.println("==========comlist by query======"+list.get(i).getComName());
			
		}
        
		

		return list;
	}
	/**
	 * 查找并返回所有价格记录数
	 */

	public List<ZynPrice2> priceTatolRows(ZynQueryPrice queryCrt) {
		getSession().clear();

		ActionContext actionContext = ActionContext.getContext();         
	     Map session = actionContext.getSession(); 
	     ZynUser loginUser=(ZynUser)session.get("loginUser");   //登录用户	
	     Criteria crt = getSession().createCriteria(ZynPrice2.class);
	     Criteria crtCom = crt.createCriteria("comObj");
	
	     
			crt.addOrder(org.hibernate.criterion.Order.desc("id"));
			if(queryCrt.getStartDate()!=null&&queryCrt.getEndDate()!=null){
				crt.add(Restrictions.and(Restrictions.ge("invalidationDate", queryCrt.getStartDate()), Restrictions.le("invalidationDate", queryCrt.getEndDate())));
			}else if(queryCrt.getStartDate()!=null&&queryCrt.getEndDate()==null){
				crt.add(Restrictions.ge("invalidationDate", queryCrt.getStartDate()));	
			}else if(queryCrt.getStartDate()==null&&queryCrt.getEndDate()!=null){
				crt.add(Restrictions.le("invalidationDate", queryCrt.getEndDate()));	
			}
			
			if(queryCrt.getFromComName()!=null&&!queryCrt.getFromComName().equalsIgnoreCase("")){ //公司
				
				crtCom.add(Restrictions.like("comName", queryCrt.getFromComName(),MatchMode.ANYWHERE));
			}
		
			if(queryCrt.getPrice()!=null&&queryCrt.getPrice()>0){ //价格
				crt.add(Restrictions.ge("price", queryCrt.getPrice()));	
			}
					
			//--权限判断,分公司只能看本公司单据，本部可以查看全部		
			if(loginUser.getZyncom().getComProperty().equalsIgnoreCase(Const.comTypetop)){			
				
			}else{			
						//所属分公司
				crtCom.add(Restrictions.eq("extAttr3", String.valueOf(loginUser.getComId())));
				  //匹配单据的所属公司
			}
			
			
			
		List<ZynPrice2> list = crt.list();
		return list;
	}
}